// Copyright 2010 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v13;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.lib.ServiceAccountantManager;
import com.google.api.adwords.v13.AdGroupEstimate;
import com.google.api.adwords.v13.AdGroupRequest;
import com.google.api.adwords.v13.CampaignEstimate;
import com.google.api.adwords.v13.CampaignRequest;
import com.google.api.adwords.v13.GeoTarget;
import com.google.api.adwords.v13.KeywordEstimate;
import com.google.api.adwords.v13.KeywordRequest;
import com.google.api.adwords.v13.KeywordType;
import com.google.api.adwords.v13.NetworkType;
import com.google.api.adwords.v13.TrafficEstimatorInterface;

/**
 * This demo shows how to use the TrafficEstimatorService.
 */
public class TrafficEstimatorDemo {
  public static void main(String[] args) {
    try {
      // Log SOAP XML request and response.
      AdWordsServiceLogger.log();

      // Get AdWordsUser from "~/adwords.properties".
      AdWordsUser user = new AdWordsUser();

      // Get TrafficEstimatorService.
      TrafficEstimatorInterface trafficEstimatorService =
          user.getService(AdWordsService.V13.TRAFFIC_ESTIMATOR_SERVICE);

      // Set the attributes of the keywords to be estimated.
      KeywordRequest keywordRequest = new KeywordRequest();
      keywordRequest.setText("sale");
      keywordRequest.setMaxCpc(new Long("1000000"));
      keywordRequest.setType(KeywordType.Broad);
      // Make an array of the KeywordRequest(s).
      KeywordRequest[] keywordList = {keywordRequest};

      AdGroupRequest adGroupRequest = new AdGroupRequest();
      adGroupRequest.setKeywordRequests(keywordList);
      adGroupRequest.setMaxCpc(new Long("1000000"));
      AdGroupRequest[] adGroupList = {adGroupRequest};

      GeoTarget geoTarget = new GeoTarget();
      geoTarget.setTargetAll(true);

      CampaignRequest campaignRequest = new CampaignRequest();
      campaignRequest.setAdGroupRequests(adGroupList);
      campaignRequest.setGeoTargeting(geoTarget);
      campaignRequest.setLanguageTargeting(new String[] {"en"});
      campaignRequest.setNetworkTargeting(new NetworkType[] {NetworkType.SearchNetwork});
      CampaignRequest[] campaignRequests = {campaignRequest};

      CampaignEstimate[] estimates =
        trafficEstimatorService.estimateCampaignList(campaignRequests);

      // Send the request to the TrafficEstimator service.
      int i = 0;
      for (CampaignEstimate cpEstimate : estimates) {
        for (AdGroupEstimate agEstimate : cpEstimate.getAdGroupEstimates()) {
          for (KeywordEstimate estimate : agEstimate.getKeywordEstimates()) {
            System.out.println("Keyword: " + keywordList[i].getText()
                + "\nLower average position is " + estimate.getLowerAvgPosition()
                + ".\nUpper average position is " + estimate.getUpperAvgPosition()
                + ".\nLower clicks per day is " + estimate.getLowerClicksPerDay()
                + ".\nUpper clicks per day is " + estimate.getUpperClicksPerDay()
                + ".\nLower cpc is " + estimate.getLowerCpc() + ".\nUpper cpc is "
                + estimate.getUpperCpc() + ".");
            i++;
          }
        }
      }

      // Determining how much quota all these operations have consumed.
      System.out.println("Total Quota unit cost for this run: "
          + ServiceAccountantManager.getInstance().getTotalUnitCount());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
